layui.use('upload', function(){
    var $ = layui.jquery
        ,upload = layui.upload;

    //单张图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        ,url: '../album-picture/image'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            if(res.code == '0'){
                return layer.msg('上传成功');
            }

            return layer.msg('上传失败');
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    //多图片上传
    upload.render({
        elem: '#test2'
        ,url: '../album-picture/images'
        ,multiple: true
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
            });
        }
        ,done: function(res){
            if(res.code == '0'){
                return layer.msg('上传成功');
            }

            return layer.msg('上传失败');
        }
    });

    //拖拽上传
    upload.render({
        elem: '#test10'
        ,url: '../album-picture/image'
        ,done: function(res){
            if(res.code == '0'){
                layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.files.file);
                return layer.msg('上传成功');
            }
            return layer.msg('上传失败');

        }
    });
});